package cn.tmall.servlet;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.tmall.bean.Category;
import cn.tmall.bean.Product;
import cn.tmall.bean.Property;
import cn.tmall.bean.PropertyValue;
import cn.tmall.dao.CategoryDAO;
import cn.tmall.dao.ProductDAO;
import cn.tmall.dao.PropertyDAO;
import cn.tmall.dao.PropertyValueDAO;
import cn.tmall.util.Page;

public class ProductServlet extends BaseBackServlet{
	public String list(HttpServletRequest request,HttpServletResponse response,Page page){
		int cid = Integer.valueOf(request.getParameter("cid"));
		
		Category c = new CategoryDAO().get(cid);
		
		int total = new ProductDAO().getTotal();
		page.setTotal(total);
		
		
		List<Product> ps = new ProductDAO().list(cid,page.getStart(),page.getCount());
		page.setParam("&cid="+cid);
		request.setAttribute("ps", ps);
		request.setAttribute("c", c);
		request.setAttribute("page", page);
		return "admin/listProduct.jsp";
		
	}
	
	public String add(HttpServletRequest request,HttpServletResponse response,Page page) throws UnsupportedEncodingException{
		int cid = Integer.valueOf(request.getParameter("cid"));
		String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
		String subTitle =new String(request.getParameter("subTitle").getBytes("ISO-8859-1"),"UTF-8");
		Float orignalPrice = Float.valueOf(request.getParameter("orignalPrice"));
		Float promotePrice = Float.valueOf(request.getParameter("promotePrice"));
		int stock = Integer.valueOf(request.getParameter("stock"));
		
		Product p = new Product();
		Category c = new CategoryDAO().get(cid);
		p.setName(name);
		p.setSubTitle(subTitle);
		p.setStock(stock);
		p.setOrignalPrice(orignalPrice);
		p.setPromotePrice(promotePrice);
		p.setCategory(c);
		p.setCreateDate(new Date());
		new ProductDAO().add(p);
		return "@admin_product_list?cid="+cid;
	}
	
	public String delete(HttpServletRequest request,HttpServletResponse response,Page page){
		int id = Integer.valueOf(request.getParameter("id"));
		Product p = new Product();
		p = new ProductDAO().get(id);
		Category c = new Category();
		c = p.getCategory();
		new ProductDAO().delete(id);
		return "@admin_product_list?cid="+c.getId();
	}
	
	public String edit(HttpServletRequest request,HttpServletResponse response,Page page){
		int id = Integer.valueOf(request.getParameter("id"));
		Product p = new Product();
		p = new ProductDAO().get(id);
		request.setAttribute("p",p);
		return "admin/editProduct.jsp";
	}
	
	public String update(HttpServletRequest request,HttpServletResponse response,Page page) throws UnsupportedEncodingException{
		int id = Integer.valueOf(request.getParameter("id"));
		int cid = Integer.valueOf(request.getParameter("cid"));
		String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
		String subTitle =new String(request.getParameter("subTitle").getBytes("ISO-8859-1"),"UTF-8");
		Float orignalPrice = Float.valueOf(request.getParameter("orignalPrice"));
		Float promotePrice = Float.valueOf(request.getParameter("promotePrice"));
		int stock = Integer.valueOf(request.getParameter("stock"));
		Product p = new Product();
		p.setId(id);
		p.setName(name);
		p.setSubTitle(subTitle);
		p.setStock(stock);
		p.setOrignalPrice(orignalPrice);
		p.setPromotePrice(promotePrice);
		p.setCategory(new CategoryDAO().get(cid));
		p.setCreateDate(new Date());
		new ProductDAO().update(p);
		
		return "@admin_product_list?cid="+cid;
	}
	
	public String editPropertyValue(HttpServletRequest request,HttpServletResponse response,Page page){
		int id = Integer.parseInt(request.getParameter("id"));
        Product p = new ProductDAO().get(id);
        request.setAttribute("p", p);
   
        new PropertyValueDAO().init(p);
         
        List<PropertyValue> pvs = new PropertyValueDAO().list(p.getId());
         
        request.setAttribute("pvs", pvs);
         
        return "admin/editProductValue.jsp"; 
		
	}
	
	public String updatePropertyValue(HttpServletRequest request,HttpServletResponse response,Page page){
		String value = request.getParameter("value");
		int pvid = Integer.valueOf(request.getParameter("pvid"));
		new PropertyValueDAO().update(pvid, value);
		
		return "%success";
		
	}
}
